<?php
/**
 * Hoffman Framework
 *
 * LICENSE
 *
 * This source file is subject to the GNU LGPL 2.1 license that is bundled
 * with this package in the file license.txt.
 * It is also available through the world-wide-web at this URL:
 * http://creativecommons.org/licenses/LGPL/2.1
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to annop@thaicyberpoint.com so we can send you a copy immediately.
 *
 * @package    Hoffman
 * @author     Ford AntiTrust
 * @copyright  Copyright (c) 2008 Ford AntiTrust (http://www.thaicyberpoint.com/ford/blog)
 * @since      Version Rv 0.2
 * @license    http://creativecommons.org/licenses/LGPL/2.1
 *             GNU LGPL 2.1
 * @filesource
 */

if (!defined('INDEX'))
    die('No direct script access allowed.');

/**
 * @see Hmf_Model
 */
require_once 'Hmf/Model.php';

/**
 * @see Hmf_Validate
 */
require_once 'Hmf/Validate.php';

/**
 * Db
 *
 * @package     Hoffman
 * @subpackage  Hmf_Model
 * @category    General
 * @author      Ford AntiTrust
 * @version     SVN: $Id: General.php 149 2009-03-01 12:18:00Z annop.s@gmail.com $
 * @link        http://www.thaicyberpoint.com/ford/blog/hmf
 */

class Hmf_Model_General implements Hmf_Model {

    private $name, $validate;

    private $hasLoadConstructor = false;

    function __construct($modelName = null) {

        if (!$this->hasLoadConstructor) {
            $this->validate = new Hmf_Validate();
            $this->name = empty($modelName) ? preg_replace('/^model_/', '', get_class($this)) : $modelName;
            $this->hasLoadConstructor = true;
        }
    }

    public function __toString() {

        return $this->name;
    }

    public function used() {

        return null;
    }

    final public function executeValidate(array $data) {

        return $this->validate->validate($data);
    }

    final public function addValidate($rule, $inputName, array $args = array(), $userErrorMessage = null) {

        $this->validate->addRule($inputName, $rule, $args, $userErrorMessage);

        return $this;
    }

    final public function getValidatedResult() {

        return $this->validate->getResults();
    }

    final public function getValidatedRules() {

        return $this->validate->getRules();
    }

    final public function getValidatedResultErrorMsg() {

        $errorout = null;
        $validateInfo = $this->getValidatedResult();
        foreach ($validateInfo as $key => $val) {
            if ($val['bool'] === false) {
                foreach ($val['message'] as $msg)
                    $errorout .= $key . ': ' . $msg . "\r\n";
            }
        }
        if (is_null($errorout)) {
            return true;
        } else {
            return $errorout;
        }
    }
    final public function isValid($keyword, $value) {

        if (!$this->validate->isValid($keyword, $value))
            return false;

        return true;
    }
}
?>